﻿#region using
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
#endregion

namespace XmlGenSharp.Logic.Main
{
  public class ConfigLocator
  {
    // Public Static Methods

    #region Locate(string solutionDir, string inputFileName)
    public static string Locate(string solutionDir, string inputFileName)
    {
      string inputName = Path.GetFileNameWithoutExtension(inputFileName);
      string inputDir = Path.GetDirectoryName(inputFileName);

      string guess = Path.Combine(inputDir, inputName + ".config");
      if (File.Exists(guess))
        return guess;

      DirectoryInfo di = new DirectoryInfo(inputDir);
      while (di != null)
      {
        guess = Path.Combine(di.FullName, "XmlGenSharp.config");
        if (File.Exists(guess))
          return guess;

        if (di != null && di.FullName == solutionDir)
          break; // Don't go above solution directory

        di = di.Parent;
      }

      // Not found
      return null;
    }
    #endregion
  }
}
